﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;

namespace ESN.Helpers
{
    public class RemoteHelper
    {
        public static string Post(string url, Dictionary<string, string> data, string type = "application/x-www-form-urlencoded")
        {
            WebRequest request = WebRequest.Create(url);
            request.Method = "POST";
            string postData = "";
            foreach (var item in data)
            {
                postData = postData + item.Key + "=" + item.Value + "&";
            }
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = type;
            request.ContentLength = byteArray.Length;

            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse response = request.GetResponse();
            dataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(dataStream);
            var responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();

            return responseFromServer;
        }

        public static string Get(string url)
        {
            WebRequest request = WebRequest.Create(url);
            request.Credentials = CredentialCache.DefaultCredentials;
            WebResponse response = request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return responseFromServer;
        }

        public static string Put(string url, byte[] data)
        {
            WebRequest request = WebRequest.Create(url);
            request.Method = "PUT";

            request.ContentLength = data.Length;

            Stream dataStream = request.GetRequestStream();
            dataStream.Write(data, 0, data.Length);
            dataStream.Close();

            WebResponse response = request.GetResponse();
            dataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(dataStream);
            var responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();

            return responseFromServer;
        }
        public static string Delete(string url)
        {
            WebRequest request = WebRequest.Create(url);
            request.Method = "DELETE";

            request.Credentials = CredentialCache.DefaultCredentials;
            WebResponse response = request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return responseFromServer;
        }
        //public static string Put(string url, byte[] data)
        //{
        //    // Create a request using a URL that can receive a post. 
        //    WebRequest request = WebRequest.Create(url);
        //    // Set the Method property of the request to POST.
        //    request.Method = "PUT";
        //    // Create POST data and convert it to a byte array.
        //    byte[] byteArray = data;

        //    // Set the ContentLength property of the WebRequest.
        //    request.ContentLength = byteArray.Length;
        //    // Get the request stream.
        //    using (Stream dataStream = request.GetRequestStream())
        //    {
        //        dataStream.Write(byteArray, 0, byteArray.Length);
        //    }

        //    // Get the response.
        //    try
        //    {
        //        using (WebResponse response = request.GetResponse())
        //        using (Stream responseStream = response.GetResponseStream())
        //        using (StreamReader reader = new StreamReader(responseStream))
        //        {
        //            string responseFromServer = reader.ReadToEnd();
        //            // Display the content.
        //            return responseFromServer;

        //        }
        //    }
        //    catch (WebException e)
        //    {
        //        using (WebResponse response = e.Response)
        //        {
        //            HttpWebResponse httpResponse = (HttpWebResponse)response;
        //            Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
        //            using (Stream bin = response.GetResponseStream())
        //            {
        //                string text = new StreamReader(bin).ReadToEnd();
        //                return text;
        //            }
        //        }
        //    }
        //}

        public static string GetRedirect(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Credentials = CredentialCache.DefaultCredentials;
            //request.AllowAutoRedirect = false;
            WebResponse response = request.GetResponse();
            return response.ResponseUri.ToString();
        }
    }
}